package com.dicti.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.config.FieldFilterUtils;
import com.dicti.Util.DictiUtils;
import com.dicti.domain.Dicti;
import com.dicti.service.DictiService;
import exam.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/4/24 15:15
 * @since JDK 1.8
 */
@RestController
@Api(tags = {"字典模块"})
@RequestMapping("dicti")
public class DictiController {

    @Autowired
    private DictiService dictiService;

    @PostMapping("namelist")
    @ApiOperation("分页获取字典模块列表")
    public JSONObject getDictiListController(@ApiParam("页码")@RequestParam(value = "pageNum",defaultValue = "1")Integer page,
                                             @ApiParam("条数")@RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,
                                             @ApiParam("模块名称")@RequestParam(value = "name",required = false)String name){
        IPage<Dicti> dictiIPage=dictiService.dictiListPage(page,pageSize,name);
        FieldFilterUtils.listFilter(new String[]{"dictiValue"},Dicti.class,dictiIPage.getRecords());
        return Result.successObject(dictiIPage.getTotal(),dictiIPage.getRecords());
    }

    @PostMapping("valueList/{id}")
    @ApiOperation("通过id获取字典对象")
    public JSONObject getVaueListController(@PathVariable("id")String id){
        Dicti dicti=dictiService.getById(id);
        Long total=Long.parseLong(dicti.getDictiValues().size()+"");
        return Result.successObject(total,dicti.getDictiValues());
    }

    @PostMapping("addName")
    @ApiOperation("创建字典模块")
    public JSONObject createDictiNameController(@ApiParam("字典模块名称")@RequestParam("name") String name){
        int num=dictiService.addDicti(name);
        if(num>0){
            return Result.successObject("字典模块创建成功");
        }
        return Result.errorObject("字典模块名称已存在");
    }

    @GetMapping("values")
    @ApiOperation("获取具体某个模块的值列表")
    public JSONObject getDictiValueListByIdController(@ApiParam("字典模块的id")@RequestParam("id")String id){
        JSONArray jsonArray= dictiService.getValuesById(id);
        return Result.successObject(jsonArray);
    }

    @PostMapping("addvaluebyid")
    @ApiOperation("添加字典的值")
    public JSONObject addDictiValueByIdController(@ApiParam("主键id")@RequestParam("id")String id,
                                                  @ApiParam("要添加的key")@RequestParam("key")String key,
                                                  @ApiParam("要添加的value")@RequestParam("value")String values){
       int code=dictiService.addDictiValueById(id,key,values);
       if(code>0){
           return Result.successObject("添加成功");
       }
       return Result.errorObject("系统错误");
    }

    @GetMapping("getvalue")
    @ApiOperation("字典模块取值")
    public JSONObject getValueByNameAndkey(@ApiParam("模块名称")@RequestParam("name") String name,
                                           @ApiParam("取值key")@RequestParam("key") String key){
        if(StringUtils.isEmpty(name)||StringUtils.isEmpty(key)){
            return Result.errorObject("必要参数不得为空");
        }
        String value=dictiService.getValueByName(name,key);
        JSONObject jsonObject=Result.successObject();
        jsonObject.put("key",key);
        jsonObject.put("value",value);
        return jsonObject;
    }






}
